package com.lxw.nonghe.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class Slf4jLogFilter implements Filter {

    private static final Logger logger = LoggerFactory.getLogger(Slf4jLogFilter.class);

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {

        HttpServletRequest httpRequest = (HttpServletRequest) request;
        long start = System.currentTimeMillis();

        try {
            chain.doFilter(request, response);
        } finally {
            long duration = System.currentTimeMillis() - start;
            logger.info("{} {} - {}ms",
                httpRequest.getMethod(),
                httpRequest.getRequestURI(),
                duration);
        }
    }
}
